// pages/selectMap/selectMap.js
// pages/navigation/navigation.js
// 引入SDK核心类
var QQMapWX = require("../../utils/js/qqmap-wx-jssdk");
// 实例化API核心类
var qqmapsdk = new QQMapWX({
  key: "R4OBZ-UAIKT-34IXQ-LGLZH-YWJES-D6BCH", // 必填
});
Page({
  data: {
    id:1,
    startLocation: {
      latitude: "27.531663",
      longitude: "115.825943",
    },
    polygon: [],
    points: [
      [
        ["27.534838", "115.830104"],
        ["27.531712", "115.819990"],
        ["27.527194", "115.822795"],
        ["27.527407", "115.833430"],
        ["27.530553", "115.835145"],
        ["27.534838", "115.830104"],
      ],
      [
        ["27.534938", "115.830204"],
        ["27.530653", "115.835245"],
        ["27.539030", "115.846655"],
        ["27.543790", "115.840184"],
        ["27.534938", "115.830204"],
      ],
      [
        ["27.530553", "115.835345"],
        ["27.511949", "115.857143"],
        ["27.515256", "115.864131"],
        ["27.522706", "115.861403"],
        ["27.538930", "115.846755"],
        ["27.530553", "115.835345"],
      ],
      [
        ["27.543890", "115.840284"],
        ["27.539130", "115.846755"],
        ["27.567325", "115.869432"],
        ["27.574170", "115.861900"],
        ["27.543890", "115.840284"],
      ],
      [
        ["27.543912", "115.850810"],
        ["27.529722", "115.866965"],
        ["27.532334", "115.880712"],
        ["27.542248", "115.876928"],
        ["27.556305", "115.860827"],
        ["27.543912", "115.850810"],
      ],
      [
        ["27.574270", "115.862000"],
        ["27.567425", "115.869532"],
        ["27.585651", "115.906046"],
        ["27.606658", "115.891407"],
        ["27.591628", "115.863931"],
        ["27.574270", "115.862000"],
      ],
      [
        ["27.531812", "115.819890"],
        ["27.534938", "115.830004"],
        ["27.551288", "115.823707"],
        ["27.550508", "115.812660"],
        ["27.531812", "115.819890"],
      ],
      [
        ["27.550608", "115.812560"],
        ["27.551388", "115.823607"],
        ["27.567308", "115.824623"],
        ["27.567142", "115.814646"],
        ["27.550608", "115.812560"],
      ],
      [
        ["27.567308", "115.824723"],
        ["27.551388", "115.823707"],
        ["27.556334", "115.849037"],
        ["27.574170", "115.861800"],
        ["27.567308", "115.824723"],
      ],
      [
        ["27.526994", "115.822795"],
        ["27.509011", "115.822795"],
        ["27.509011", "115.837116"],
        ["27.527207", "115.833430"],
        ["27.526994", "115.822795"],
      ],
      // [
      //   ["27.508811", "115.822795"],
      //   ["27.489778", "115.820040"],
      //   ["27.486356", "115.846803"],
      //   ["27.506980", "115.847099"],
      //   ["27.508811", "115.837116"],
      //   ["27.508811", "115.822795"],
      // ],
      // [
      //   ["27.506980", "115.847299"],
      //   ["27.486356", "115.847003"],
      //   ["27.486356", "115.871883"],
      //   ["27.496980", "115.871883"],
      //   ["27.506980", "115.847299"],
      // ],
      // [
      //   ["27.489778", "115.820040"],
      //   ["27.485159", "115.804720"],
      //   ["27.469949", "115.804720"],
      //   ["27.472498", "115.850161"],
      //   ["27.486156", "115.846803"],
      //   ["27.489778", "115.820040"],
      // ],
      // [
      //   ["27.508911", "115.822595"],
      //   ["27.512177", "115.814170"],
      //   ["27.509264", "115.793664"],
      //   ["27.497854", "115.793664"],
      //   ["27.489978", "115.819840"],
      //   ["27.508911", "115.822595"],
      // ],
      // [
      //   ["27.541094", "115.816066"],
      //   ["27.528167", "115.793664"],
      //   ["27.519023", "115.807297"],
      //   ["27.519023", "115.814533"],
      //   ["27.531812", "115.819690"],
      // ],
    ],
    pointList: [
      {
        latitude: "27.530927",
        longitude: "115.827565",
        name: "#1001",
        full: true,
        select: false,
      },
      {
        latitude: "27.537171",
        longitude: "115.837816",
        name: "#1002",
        full: false,
        select: false,
      },
      // 石洞
      {
        latitude: "27.525448",
        longitude: "115.850361",
        name: "#1003",
        full: false,
        select: false,
      },
      // 湖溪村
      {
        latitude: "27.555478",
        longitude: "115.854530",
        name: "#1004",
        full: false,
        select: false,
      },
      // 书塘
      {
        latitude: "27.540226",
        longitude: "115.865555",
        name: "#1005",
        full: false,
        select: false,
      },
      // 甘泉
      {
        latitude: "27.587979823519078",
        longitude: "115.88252071353759",
        name: "#1006",
        full: false,
        select: false,
      },
      // 东堆
      {
        latitude: "27.542072204610673",
        longitude: "115.82151234642538",
        name: "#1007",
        full: false,
        select: false,
      },
      // 梅溪
      {
        latitude: "27.55885492313942",
        longitude: "115.81903590695902",
        name: "#1008",
        full: false,
        select: false,
      },
      // 河元背
      {
        latitude: "27.56219677598865",
        longitude: "115.8382909208882",
        name: "#1009",
        full: false,
        select: false,
      },
      // 社背
      {
        latitude: "27.517783131064405",
        longitude: "115.82897282256545",
        name: "#1010",
        full: false,
        select: false,
      },
      // 沙港
      // {
      //   latitude: "27.498265301638973",
      //   longitude: "115.83429732610921",
      //   name: "#1011",
      //   full: false,
      //   select: false,
      // },
      // // 上舍
      // {
      //   latitude: "27.494257117566185",
      //   longitude: "115.8579216635843",
      //   name: "#1012",
      //   full: false,
      //   select: false,
      // },
      // // 康村
      // {
      //   latitude: "27.47937558790555",
      //   longitude: "115.82725101402684",
      //   name: "#1013",
      //   full: false,
      //   select: false,
      // },
      // // 饶门
      // {
      //   latitude: "27.502824491056973",
      //   longitude: "115.80788112523658",
      //   name: "#1014",
      //   full: true,
      //   select: false,
      // },
      // // 王龙
      // {
      //   latitude: "27.529074287900166",
      //   longitude: "115.80831651599101",
      //   name: "#1015",
      //   full: false,
      //   select: false,
      // },
    ],
    markers: [],
    fullColor: "#0089ff",
    noFullColor: "#1aad19",
    selectColor: "#0089ff",
    noSelectColor: "#f2f1ed",
  },

  onLoad(options) {
    console.log(options)
    let id = JSON.parse(options.id);
    this.data.id=id;
    // this.data.campid = JSON.parse(data.campid)
    this.inintMap();
    this.showMarkers();
  },
  onShow() {},

  //点击地图
  getLocation(e) {
    console.log(e.detail);
  },
  //点击标点
  clickMarker(e) {
    // console.log(e);
    let index = e.detail.markerId;
    let pointList = this.data.pointList;
    pointList.forEach(item => {
      item.select = false
    })
    pointList[Number(index)].select = !pointList[Number(index)].select;
    this.setData({
      pointList: pointList,
    });
    this.inintMap();
  },
  //展示标点
  showMarkers() {
    let pointList = this.data.pointList;
    var markers = [];
    for (let i = 0; i < pointList.length; i++) {
      let marker = {
        id: 0,
        latitude: 0,
        longitude: 0,
        iconPath: "/image/icon/location.png",
        height: 10,
        width: 10,
        callout: {},
      };
      marker.id = i;
      marker.latitude = pointList[i].latitude;
      marker.longitude = pointList[i].longitude;
      let bgColor = pointList[i].full
        ? this.data.fullColor
        : this.data.noFullColor;
      marker.callout = {
        content: pointList[i].name,
        bgColor: bgColor,
        color: "#fff",
        fontSize: "14",
        textAlign: "center",
        display: "ALWAYS",
        anchorY: 30,
        borderRadius: 6,
        padding: 6,
      };
      markers.push(marker);
    }
    this.setData({
      markers: markers,
    });
  },
  //显示地图
  inintMap() {
    let basePolygonOne = {
      strokeWidth: 3,
      fillColor: "#f2f1ed",
      strokeColor: "#1aad19",
    };
    let pointsDate = this.data.points;
    let polygon = [];
    pointsDate.forEach((item1, index1) => {
      let pointOne = [];
      item1.forEach((item2, index2) => {
        let point = {
          latitude: item2[0],
          longitude: item2[1],
        };
        pointOne.push(point);
      });
      let polyonOne = { ...basePolygonOne };
      polyonOne.points = pointOne;
      let fillColor = this.data.pointList[index1].select
        ? this.data.selectColor
        : this.data.noSelectColor;
      polyonOne.fillColor = fillColor;
      polygon.push(polyonOne);
    });
    this.setData({
      polygon: polygon,
    });
  },
  //确定
  selectComfirm() {
    // console.log(this.data.pointList);
    let pointList = this.data.pointList;
    // let selectList = [];
    let name="";
    pointList.forEach((item, index) => {
      if (item.select) {
        name=item.name;

      }
    });
    let data={
      name:name,
      id:this.data.id
    }
    data = JSON.stringify(data)
    // wx.showToast({
    //   title: "正在生成路线",
    //   icon: "error",
    //   duration: 2000,
    //   success(res) {
    //     let data = JSON.stringify(selectList);
        wx.navigateTo({
          url: "/pages/index/object/object?data=" + data,
        });
      // },
    // });
  },
  //取消
  selectCancel() {
    wx.navigateBack();
  },
});
